package com.hainatong.shiphelper.module.handlejob.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import androidx.core.content.ContextCompat;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.module.handlejob.entity.HandleParam;
import com.hainatong.shiphelper.module.handlejob.viewmodel.HandleJobViewModel;
import com.hainatong.shiphelper.utils.GsonUtil;
import com.hainatong.shiphelper.utils.ImageUtils;
import com.hainatong.shiphelper.utils.PushToast;
import com.hainatong.shiphelper.widget.HandSignatureView;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentSigntureBinding;
import com.hainatong.shiphelper.widget.LoadingDialog;
import com.hainatong.shiphelper.widget.passwardview.FinishInputPassword;
import com.hainatong.shiphelper.widget.passwardview.PassWordFragment;

import java.io.File;


/**
 * 签名 验证密码 提交 交接信息
 */
public class SignatureFragment  extends MvvmLazyFragment<FragmentSigntureBinding , HandleJobViewModel>
{
    private static String SIGNATURE_TAG = "SignatureFragment";

    //SignatureView
    private HandSignatureView signView;

    // 提交的 交接 任务参数
    private HandleParam handleParam;

    private LoadingDialog loading;

    public static SignatureFragment newInstance(String typeCode)
    {
        Bundle bundle = new Bundle();
        bundle.putString( SIGNATURE_TAG , typeCode );
        SignatureFragment signFragment = new SignatureFragment();
        signFragment.setArguments( bundle );
        return signFragment;
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_signture;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {

    }

    @Override
    protected void initParameters()
    {
        Bundle args = getArguments();
        String type = "";
        if(  args != null )
        {
            type = args.getString(SIGNATURE_TAG , "");
        }

        if( !StringUtils.isEmpty( type ))
        {
            handleParam = GsonUtil.json2Obj(type, HandleParam.class);
        }
    }

    @Override
    protected void initView(View view)
    {
        loading = new LoadingDialog(getActivity() ,getString( R.string.loading ) );
        signView = new HandSignatureView(getActivity() , ContextCompat.getColor(getActivity(),R.color.text_color));

        viewDataBinding.flView.addView( signView );
        signView.requestFocus();

        viewDataBinding.btnRewrite.setOnClickListener( v -> {
            signView.clear();
        } );

        viewDataBinding.btnSubmit.setOnClickListener( v -> {


            //显示输入密码框  先验证密码
            PassWordFragment.newInstace(new FinishInputPassword() {
                @Override
                public void finishPassword(String password)
                {
                    ToastUtils.showShort( "提交交接任务" );

                    //signView.getCacheBitmap()
                    Bitmap signBitmap = com.blankj.utilcode.util.ImageUtils.view2Bitmap( viewDataBinding.flView );
                    // 预览小图
                    //viewDataBinding.ivSign.setImageBitmap( signBitmap );
                    File signImgFile = ImageUtils.newImgFileName() ;
                    // 保存 到 相册
                    boolean isSave = ImageUtils.saveImageToGallery( getActivity() ,signBitmap , signImgFile);
                    if( isSave )
                    {
                        //上传图片
                        loading.show();
                        viewModel.upLoadImg( signImgFile );
                    }
                }
                @Override
                public void clickForgetPassword()
                {
                    ToastUtils.showShort( "请联系管理员" );
                }
            }).show(getParentFragmentManager(),"PassWordFragment");

        } );

    }

    @Override
    protected void lazyLoad()
    {
        //上传的图片
        viewModel.getUpImgResult().observe( this,result -> {

            if( result.isSuccess() )
            {
                handleParam.setSign_file_id( result.getData().getUuid() );
                viewModel.submitHandleJobTask( handleParam );
            }
            else
            {
                loading.hide();
                ToastUtils.showShort( result.getMsg() );
            }
        } );

        // 提交任务 结果
        viewModel.getHandleTaskResult().observe(this,result -> {
            if( loading.isShowing() )
            {
                loading.hide();
            }
            if( result.isSuccess() )
            {
                PushToast.getInstance(getActivity()).show(PushToast.SUBMIT_SUCCESS);
            }
            else
            {
                ToastUtils.showShort( result.getMsg() );
            }
        });
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        loading.dismiss();
    }
}
